// 城市类
import { BaseModel } from "./BaseModel";
import * as THREE from "three";
// 边缘边线类
import { EdgesLine } from "../effect/EdgesLine";
// 修改城市模型的着色器函数
import { modifyCityDefaultMaterial } from "../shader/modifyCityMaterial";
// 引入计算物体中心点和大小的工具函数
import { getBoxCenter } from "../utils/getBoxCenter";
// 火灾类
import { Fire } from "../effect/Fire";
// 火灾影响范围球体标记
import { FireBall } from "../effect/FireBall";
// 建筑物信息标记
import { BuildInfo } from "../dom/BuildInfo";
// 动效管理类
import { EffectManager } from "../utils/EffectManager";
// 单击事件管理类
import { ClickHandler } from "../utils/clickHandler";

export class City extends BaseModel {
  init() {
    this.scene.add(this.model);
    this.initEffect()
    this.initFire('01-shanghaizhongxindasha')

    this.buildNameObj = { // 模型名字和建筑显示名字对应关系
      '01-shanghaizhongxindasha': '上海中心大厦',
      "02-huanqiujinrongzhongxin": "环球金融中心",
      "03-jinmaodasha": "金茂大厦",
      "04-dongfangmingzhu": "东方明珠"
    }
    this.bindClick()
  }
  // 初始化城市效果
  initEffect() {
    // 中心城市建筑材质
    const centerMaterial = new THREE.MeshBasicMaterial({
      color: 0xa8cded,
      transparent: true,
    });
    // 外围城市建筑材质
    const periphery = new THREE.MeshBasicMaterial({
      color: 0xa8cded,
      transparent: true,
    });
    this.model.trverse((model) => {
      if (model.name == "Text") {
        // 隐藏默认建筑名字标签
        model.visible = false;
        return;
      }

      // 除开地面和河流
      if (
        model.name !== "Shanghai-09-Floor" &&
        model.name !== "Shanghai-08-River"
      ) {
        // 修改城市建筑模型材质
        if (
          model.name === "Shanghai-02" ||
          model.name === "Shanghai-03" ||
          model.name === "Shanghai-04"
        ) {
          // 周围建筑材质
          model.material = periphery;
          new EdgesLine(this.scene, model, new THREE.Color("#666666"));
          // 对物体添加渐变色白膜效果
          modifyCityDefaultMaterial(model, false)
        } else {
          // 中心建筑材质
          model.material = centerMaterial;
          new EdgesLine(this.scene, model, new THREE.Color("#00ffffff"));
          // 对物体添加渐变色白膜效果
          modifyCityDefaultMaterial(model, true)
        }
      }

      // 针对水物体单独处理
      if (model.name === 'Shangehai-08-River') {
        //把原本水物体隐藏
        model.visible = false
        // 创建更加真实的水面效果物体
        const theWater = new CityWater(model, this.scene)
        // 把水波纹物体传入到动效管理类当中
        EffectManager.getInstance().addObj(theWater)
      }

    });
  }

  // 创建火灾标记
  initFire(buildName) {
    const build = this.model.getObjectByName(buildName)
    const { center, size } = getBoxCenter(build)

    new Fire(this.scene, center, size) // 火灾精灵物体
    new FireBall(this.scene, center) // 火灾影响范围球体
  }

  // 中心 4 个建筑绑定点击事件
  bindClick() {
    Object.keys(this.buildNameObj).forEach(key => {
      const build = this.model.getObjectByName(key)
      ClickHandler.getInstance().addMesh(build, (object) => {
        // object: 3d 物体
        const { center } = getBoxCenter(object)
        new BuildInfo(this.scene, center, {
          "squareMeters": "500",
          "name": this.buildNameObj[object.name], // 点击的建筑物的名字作为key, 去名字对象中换出中文的名字
          "officesRemain": "500",
          "accommodate": "500",
          "parkingRemain": "88",
        })
      })
    })
  }


}
